package com.sgb.knowledgeshare.view.activity;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import android.text.TextUtils;
import android.view.View;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.knowledgeshare.weight.SlideBackActivity;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.ActivityShareUserBinding;
import com.sgb.knowledgeshare.entity.ShareUserInfoEntity;
import com.sgb.knowledgeshare.view.fragment.ShareUserListFragment;
import com.sgb.knowledgeshare.viewmodel.ShareUserModel;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * Created by fq on 2020/9/11 0011
 * Description：分享-单独的用户界面：为了实现多层点击
 */
public class ShareUserActivity  extends SlideBackActivity {
    private static final String USERID = "userid";
    private ActivityShareUserBinding binding;
    ShareUserModel viewmodel;
    private ArrayList<Fragment> mFragments;
    private String[] titles;
    private boolean islooks=false;

    public static void start(Context context,String userid) {
        Intent intent = new Intent();
        intent.putExtra(USERID,userid);
        intent.setClass(context, ShareUserActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        viewmodel.getUserHomePage(getIntent().getStringExtra(USERID));
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_share_user);
        viewmodel = new ViewModelProvider(this).get(ShareUserModel.class);
        viewmodel.setActivity(ShareUserActivity.this);
        binding.llShareUserLike.setOnClickListener(this);//获赞
        binding.llShareUserAttention.setOnClickListener(this);//关注
        binding.llShareUserFans.setOnClickListener(this);//粉丝
        binding.backView.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        int id = v.getId();//点赞
        if (id == R.id.ll_share_user_like) {
        } else if (id == R.id.ll_share_user_attention) {//关注
            ShareUserAttentionActivity.start(this, getIntent().getStringExtra(USERID));
        } else if (id == R.id.ll_share_user_fans) {//粉丝
            ShareUserFansActivity.start(this, getIntent().getStringExtra(USERID));
        } else if (id == R.id.back_view) {//返回
            finish();
        }
    }

    public void setUserData(ShareUserInfoEntity data) {
//        头像
        if (!TextUtils.isEmpty(data.getPhoto())) {
            Glide.with(getApplicationContext()).load(data.getPhoto() + "")
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()).placeholder(R.mipmap.share_def_photo))
                    .into(binding.shareUserLogo);
        }
//        是否关注状态   0未关注、1已关注、2互相关注
//        share_user_state
        int isUserHomePage = data.getIsUserHomePage();
        if (isUserHomePage == 1) {//是否时自己的主页
            binding.shareUserState.setVisibility(View.GONE);
        } else {
            binding.shareUserState.setVisibility(View.VISIBLE);
        }
        switch (data.getIsFollow()) {
            case 0:
                binding.shareUserState.setText("关注");
                binding.shareUserState.setBackgroundColor(getResources().getColor(R.color.color_5792FD));
                break;
            case 1:
                binding.shareUserState.setText("取消关注");
                binding.shareUserState.setBackgroundColor(getResources().getColor(R.color.color_C0C4CC));
                break;
            case 2:
                binding.shareUserState.setText("互相关注");
                binding.shareUserState.setBackgroundColor(getResources().getColor(R.color.color_FFC000));
                break;
        }
//        用户名称
        if (!TextUtils.isEmpty(data.getName())) {
            binding.shareUserName.setText(data.getName());
        } else {
            binding.shareUserName.setText("");
        }
        //公司名称
        if (!TextUtils.isEmpty(data.getCompanyName())) {
            binding.shareUserCompany.setText(data.getCompanyName());
        } else {
            binding.shareUserCompany.setText("");
        }
        DecimalFormat df = new DecimalFormat("##0.0");
        //获赞数量
        if (data.getLaudCount() > 9999) {
            double commentnum = data.getLaudCount() / 10000;
            binding.shareUserLike.setText(df.format(commentnum) + "w");
        } else {
            binding.shareUserLike.setText(data.getLaudCount() + "");
        }
        //关注数量
        if (data.getFollow() > 9999) {
            double Follow = data.getFollow() / 10000;
            binding.shareUserAttention.setText(df.format(Follow) + "w");
        } else {
            binding.shareUserAttention.setText(data.getFollow() + "");
        }
        //粉丝数量
        if (data.getFens() > 9999) {
            double Fensnum = data.getFens() / 10000;
            binding.shareUserFans.setText(df.format(Fensnum) + "w");
        } else {
            binding.shareUserFans.setText(data.getFens() + "");
        }
        titles = new String[]{"作品  " + data.getCreateCount(), "喜欢  " + data.getLikeCount()};
        if (!islooks){
            initListView(data);
        }
    }

    private void initListView(ShareUserInfoEntity data) {
        islooks =true;
        mFragments = new ArrayList<>();
        mFragments.add(ShareUserListFragment.newInstance(data.getUserNo(), 0));
        mFragments.add(ShareUserListFragment.newInstance(data.getUserNo(), 1));
        SlidingFragmentPagerAdapter mAdapetr = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, titles);
        binding.viewpager.setAdapter(mAdapetr);
        binding.viewpager.setOffscreenPageLimit(titles.length - 1);
        binding.tablayout.setViewPager(binding.viewpager);
        binding.tablayout.setCurrentTab(0);
        binding.tablayout.notifyDataSetChanged();
//        关注的点击事件
        binding.shareUserState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    if (SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH, false)) {
                        //请求接口数据、成功后再获取数据
                        viewmodel.getFens(getIntent().getStringExtra(USERID));
                    } else {
                        MToast.showToast(getApplicationContext(), "请实名认证完成后再进行点赞！");
                    }
                } else {
                    MToast.showToast(getApplicationContext(), "请先登录账户！");
                }
            }
        });
    }

    public void UpdataUserIfno() {
        viewmodel.getUserHomePage(getIntent().getStringExtra(USERID));
    }
}
